home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261exe.zip / TYPE1OPS.PS < prev    next >
Text File  |  1993-05-28  |  7KB  |  192 lines

  1. %    Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2. %
  3. % This file is part of Ghostscript.
  4. %
  5. % Ghostscript is distributed in the hope that it will be useful, but
  6. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. % to anyone for the consequences of using it or for whether it serves any
  8. % particular purpose or works at all, unless he says so in writing.  Refer
  9. % to the Ghostscript General Public License for full details.
  10. %
  11. % Everyone is granted permission to copy, modify and redistribute
  12. % Ghostscript, but only under the conditions described in the Ghostscript
  13. % General Public License.  A copy of this license is supposed to have been
  14. % given to you along with Ghostscript so you can know your rights and
  15. % responsibilities.  It should be in a file named COPYING.  Among other
  16. % things, the copyright notice and this notice must be preserved on all
  17. % copies.
  18.  
  19. % type1ops.ps
  20. % Define the Type 1 font opcodes for use by Ghostscript utilities.
  21.  
  22. % Define lenIV (the number of initial random bytes in the encoded outlines).
  23. % This should be zero, but we set it to 4 for compatibility with PostScript.
  24.  
  25. /lenIV 4 def
  26.  
  27. % Define the Type 1 opcodes we care about.
  28.  
  29. /Type1encode 25 dict
  30. /c_hstem 1 def   dup /hstem <01> put
  31. /c_vstem 3 def   dup /vstem <03> put
  32. /c_vmoveto 4 def   dup /vmoveto <04> put
  33. /c_rlineto 5 def   dup /rlineto <05> put
  34. /c_hlineto 6 def   dup /hlineto <06> put
  35. /c_vlineto 7 def   dup /vlineto <07> put
  36. /c_rrcurveto 8 def   dup /rrcurveto <08> put
  37. /c_closepath 9 def   dup /closepath <09> put
  38. /c_callsubr 10 def   /s_callsubr <0a> def   dup /callsubr s_callsubr put
  39. /c_return 11 def   dup /return <0b> put
  40. /c_escape 12 def
  41.   /ce_dotsection 0 def   /s_dotsection <0c06> def   dup /dotsection s_dotsection put
  42.   /ce_vstem3 1 def   /s_vstem3 <0c01> def   dup /vstem3 s_vstem3 put
  43.   /ce_hstem3 2 def   /s_hstem3 <0c02> def   dup /hstem3 s_hstem3 put
  44.   /ce_seac 6 def   /s_seac <0c06> def   dup /seac s_seac put
  45.   /ce_sbw 7 def   /s_sbw <0c07> def   dup /sbw s_sbw put
  46.   /ce_div 12 def   /s_div <0c0c> def   dup /div s_div put
  47.   /ce_callothersubr 16 def   /s_callothersubr <0c10> def   dup /callothersubr s_callothersubr put
  48.   /ce_pop 17 def   /s_pop <0c11> def   dup /pop s_pop put
  49.   /ce_setcurrentpoint 33 def   /s_setcurrentpoint <0c21> def   dup /setcurrentpoint s_setcurrentpoint put
  50. /c_hsbw 13 def   /s_hsbw <0d> def   dup /hsbw s_hsbw put
  51. /c_endchar 14 def   /s_endchar <0e> def   dup /endchar s_endchar put
  52. /c_rmoveto 21 def   dup /rmoveto <15> put
  53. /c_hmoveto 22 def   dup /hmoveto <16> put
  54.   /s_setcurrentpoint_hmoveto s_setcurrentpoint <8b16> concatstrings def
  55. /c_vhcurveto 30 def   dup /vhcurveto <1e> put
  56. /c_hvcurveto 31 def   dup /hvcurveto <1f> put
  57. def
  58.  
  59. % Define the encoding of numbers.
  60. /c_num1 32 def
  61. /c_num2 247 def
  62. /c_num3 251 def
  63. /c_num4 255 def
  64. /min_enc_num1 c_num1 c_num2 sub 1 add 2 idiv def
  65. /max_enc_num1 c_num2 c_num1 sub 1 sub 2 idiv def
  66. /min_enc_num2 max_enc_num1 1 add def
  67. /max_enc_num2 min_enc_num2 c_num3 c_num2 sub 256 mul add 1 sub def
  68. /min_enc_num3 max_enc_num2 neg def
  69. /max_enc_num3 min_enc_num2 neg def
  70.  
  71. % ------ CharString encoding ------ %
  72.  
  73. % For these utilities, a CharString is represented by a sequence of
  74. % integers or names, either in an array or on the stack.
  75. % Integers represent themselves; names are CharString operator names.
  76. % A CharString in an array is called a "charproc"; a CharString on
  77. % the stack is called a "charstack", and is delimited by a mark.
  78. % Individual elements are called "chartokens".
  79.  
  80. % Compute the length of a CharString.
  81. /chartoken_length    % chartoken -> length
  82.  { dup type /nametype eq
  83.     { Type1encode exch get length
  84.     }
  85.     { dup dup -107 ge exch 107 le and
  86.        { pop 1
  87.        }
  88.        { dup -1131 ge exch 1131 le and { 2 } { 5 } ifelse
  89.        }
  90.       ifelse
  91.     }
  92.    ifelse
  93.  } bind def
  94. /charproc_length    % charproc -> length
  95.  { 0 exch { chartoken_length add } forall
  96.  } bind def
  97. /charstack_length    % charstack -> charstack length
  98.  { counttomark 0 exch -1 1 { index chartoken_length add } for
  99.  } bind def
  100.  
  101. % Write a CharString to a file.  Normally this will be a NullEncode filter
  102. % writing on a string of the correct length.
  103. /chartoken_write    % file chartoken ->
  104.  { dup type /nametype eq
  105.     { Type1encode exch get writestring
  106.     }
  107.     { dup dup -107 ge exch 107 le and
  108.        { 139 add
  109.        }
  110.        { dup dup -1131 lt exch 1131 gt or
  111.       { 1 index 255 write
  112.         2 copy -24 bitshift 255 and write
  113.         2 copy -16 bitshift 255 and write
  114.         2 copy -8 bitshift 255 and write
  115.         255 and
  116.       }
  117.       { dup 0 ge { 16#f694 } { neg 16#fa94 } ifelse add
  118.         2 copy -8 bitshift write 255 and
  119.       }
  120.      ifelse
  121.        }
  122.       ifelse write
  123.     }
  124.    ifelse
  125.  } bind def
  126. /charproc_write        % file charproc ->
  127.  { { 1 index exch chartoken_write } forall pop
  128.  } bind def
  129. /charstack_write    % charstack file ->
  130.  { counttomark 1 sub -1 1 { index 1 index exch chartoken_write } for
  131.    cleartomark
  132.  } bind def
  133.  
  134. % Convert a charproc or charstack to a proper encrypted CharString.
  135. /charproc_string    % charproc -> string
  136.  { mark exch aload pop charstack_string
  137.  } bind def
  138. /charstack_string    % charstack -> string
  139.  { charstack_length lenIV add string
  140.    dup dup length lenIV sub lenIV exch getinterval    % skip lenIV
  141.    /NullEncode filter
  142.    exch 1 index counttomark 1 add 2 roll
  143.    charstack_write closefile
  144. %   4330 exch dup .type1encrypt exch pop readonly
  145.  } bind def
  146.  
  147. % ------ CharString decoding ------ %
  148.  
  149. /Type1decode 256 array
  150. dup 0 32 getinterval
  151.   /-0- /hstem /-2- /vstem /vmoveto /rlineto /hlineto /vlineto
  152.   /rrcurveto /closepath /callsubr /return null /hsbw /endchar /-15-
  153.   /-16- /-17- /-18- /-19- /-20- /rmoveto /hmoveto /-23-
  154.   /-24- /-25- /-26- /-27- /-28- /-29- /vhcurveto /hvcurveto
  155. 33 -1 roll astore pop
  156. dup 12
  157.  { dup read pop dup Type1escape exch known
  158.     { Type1escape exch get }
  159.     { =string cvs copystring }
  160.    ifelse exch
  161.  } put
  162. 32 1 246 { 2 copy dup 139 sub put pop } for
  163. dup 247 { dup read pop 108 add exch } put
  164. dup 248 { dup read pop 364 add exch } put
  165. dup 249 { dup read pop 620 add exch } put
  166. dup 250 { dup read pop 876 add exch } put
  167. dup 251 { dup read pop 108 add neg exch } put
  168. dup 252 { dup read pop 364 add neg exch } put
  169. dup 253 { dup read pop 620 add neg exch } put
  170. dup 254 { dup read pop 876 add neg exch } put
  171. dup 255 { 0 4 { 8 bitshift 1 index read pop add } repeat exch } put
  172. def
  173. /Type1escape 9 dict
  174.   dup ce_dotsection /dotsection put
  175.   dup ce_vstem3 /vstem3 put
  176.   dup ce_hstem3 /hstem3 put
  177.   dup ce_seac /seac put
  178.   dup ce_sbw /sbw put
  179.   dup ce_div /div put
  180.   dup ce_callothersubr /callothersubr put
  181.   dup ce_pop /pop put
  182.   dup ce_setcurrentpoint /setcurrentpoint put
  183. def
  184.  
  185. % Decode a CharString (unencrypted).
  186. /charstack_read        % file -> (NO MARK) charstack
  187.  { { dup read not { pop exit } if
  188.      Type1decode exch get
  189.      dup type /arraytype eq { exec } { exch } ifelse
  190.    } loop
  191.  } bind def
  192.